﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MPMusicVids.Importer;
using MPMusicVids.Data;

namespace MPMusicVids
{
    public partial class NewArtist : Form
    {
        ArtistSearch search = new ArtistSearch();
        Artist thisArtist;
        string[] results, selectedArtist;
        DataManager dm = MusicVidsMain.dm;

        public NewArtist(Artist artist)
        {
            thisArtist = artist;
            InitializeComponent();
            results = search.GetArtists(thisArtist.ToString());
            LoadArtist();
            LoadBox(results);
        }

        private void LoadBox(string[] results)
        {
            mtchList.Items.Clear();
            foreach (string result in results)
            {
                mtchList.Items.Add(result.Split(';')[0]);
            }
        }

        private void LoadArtist()
        {
            nameText.Text = thisArtist.ToString();
            bioText.Text = thisArtist.Bio;
        }

        private void mtchList_SelectedIndexChanged(object sender, EventArgs e)
        {
            bioText.Text = nameText.Text = "Loading, Please wait...";
            int i = mtchList.SelectedIndex;
            string selArtist = results[i];
            selectedArtist = search.GetArtistInfo(selArtist.Split(';')[1], selArtist.Split(';')[0]);
            thisArtist = new Artist(thisArtist.ID, selectedArtist[0], selectedArtist[1], selectedArtist[2]);
            LoadArtist();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            thisArtist.Bio = bioText.Text;
            thisArtist.Name = nameText.Text;
            //Save thisArtist to DB
            int worked = dm.addArtist(thisArtist);
            if (worked != 1)
                MessageBox.Show("Error");
            else
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void srchButton_Click(object sender, EventArgs e)
        {
            results = search.GetArtists(searchText.Text);
            LoadBox(results);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }        
        
    }
}
